home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / Zoners Half-Life Tools / common / mathlib.h < prev    next >
C/C++ Source or Header  |  2001-04-18  |  5KB  |  224 lines

  1. #ifndef MATHLIB_H__
  2. #define MATHLIB_H__
  3.  
  4. #if _MSC_VER >= 1000
  5. #pragma once
  6. #endif
  7.  
  8. #ifdef HAVE_CONFIG_H
  9. #include "config.h"
  10. #endif
  11.  
  12. #ifdef STDC_HEADERS
  13. #include <math.h>
  14. #include <float.h>
  15. #endif
  16.  
  17. #include <algorithm>
  18.  
  19. #if !defined(max) 
  20. #define max(a,b)            (((a) > (b)) ? (a) : (b))
  21. #endif
  22.  
  23. #if !defined(min)
  24. #define min(a,b)            (((a) < (b)) ? (a) : (b))
  25. #endif
  26.  
  27. #define    Q_PI    3.14159265358979323846
  28.  
  29. extern const vec3_t vec3_origin;
  30.  
  31. #define NORMAL_EPSILON   0.00001
  32. #define ON_EPSILON       0.01
  33. #define EQUAL_EPSILON    0.001
  34.  
  35.  
  36. //
  37. // Vector Math
  38. //
  39.  
  40.  
  41. #define DotProduct(x,y) ( (x)[0] * (y)[0] + (x)[1] * (y)[1]  +  (x)[2] * (y)[2])
  42. #define CrossProduct(a, b, dest) \
  43. { \
  44.     (dest)[0] = (a)[1] * (b)[2] - (a)[2] * (b)[1]; \
  45.     (dest)[1] = (a)[2] * (b)[0] - (a)[0] * (b)[2]; \
  46.     (dest)[2] = (a)[0] * (b)[1] - (a)[1] * (b)[0]; \
  47. }
  48.  
  49. #define VectorMidpoint(a,b,c)    { (c)[0]=((a)[0]+(b)[0])/2; (c)[1]=((a)[1]+(b)[1])/2; (c)[2]=((a)[2]+(b)[2])/2; }
  50.  
  51. #define VectorFill(a,b)          { (a)[0]=(b); (a)[1]=(b); (a)[2]=(b);}
  52. #define VectorAvg(a)             ( ( (a)[0] + (a)[1] + (a)[2] ) / 3 )
  53.  
  54. #define VectorSubtract(a,b,c)    { (c)[0]=(a)[0]-(b)[0]; (c)[1]=(a)[1]-(b)[1]; (c)[2]=(a)[2]-(b)[2]; }
  55. #define VectorAdd(a,b,c)         { (c)[0]=(a)[0]+(b)[0]; (c)[1]=(a)[1]+(b)[1]; (c)[2]=(a)[2]+(b)[2]; }
  56. #define VectorMultiply(a,b,c)    { (c)[0]=(a)[0]*(b)[0]; (c)[1]=(a)[1]*(b)[1]; (c)[2]=(a)[2]*(b)[2]; }
  57. #define VectorDivide(a,b,c)      { (c)[0]=(a)[0]/(b)[0]; (c)[1]=(a)[1]/(b)[1]; (c)[2]=(a)[2]/(b)[2]; }
  58.  
  59. #define VectorSubtractVec(a,b,c) { (c)[0]=(a)[0]-(b); (c)[1]=(a)[1]-(b); (c)[2]=(a)[2]-(b); }
  60. #define VectorAddVec(a,b,c)      { (c)[0]=(a)[0]+(b); (c)[1]=(a)[1]+(b); (c)[2]=(a)[2]+(b); }
  61. #define VecSubtractVector(a,b,c) { (c)[0]=(a)-(b)[0]; (c)[1]=(a)-(b)[1]; (c)[2]=(a)-(b)[2]; }
  62. #define VecAddVector(a,b,c)      { (c)[0]=(a)+(b)[0]; (c)[1]=(a)[(b)[1]; (c)[2]=(a)+(b)[2]; }
  63.  
  64. #define VectorMultiplyVec(a,b,c) { (c)[0]=(a)[0]*(b);(c)[1]=(a)[1]*(b);(c)[2]=(a)[2]*(b); }
  65. #define VectorDivideVec(a,b,c)   { (c)[0]=(a)[0]/(b);(c)[1]=(a)[1]/(b);(c)[2]=(a)[2]/(b); }
  66.  
  67. #define VectorScale(a,b,c)       { (c)[0]=(a)[0]*(b);(c)[1]=(a)[1]*(b);(c)[2]=(a)[2]*(b); }
  68.  
  69. #define VectorCopy(a,b) { (b)[0]=(a)[0]; (b)[1]=(a)[1]; (b)[2]=(a)[2]; }
  70. #define VectorClear(a)  { (a)[0] = (a)[1] = (a)[2] = 0.0; }
  71.  
  72. #define VectorMaximum(a) ( max( (a)[0], max( (a)[1], (a)[2] ) ) )
  73. #define VectorMinimum(a) ( min( (a)[0], min( (a)[1], (a)[2] ) ) )
  74.  
  75. #define VectorInverse(a) \
  76. { \
  77.     (a)[0] = -((a)[0]); \
  78.     (a)[1] = -((a)[1]); \
  79.     (a)[2] = -((a)[2]); \
  80. }
  81. #define VectorRound(a) floor((a) + 0.5)
  82. #define VectorMA(a, scale, b, dest) \
  83. { \
  84.     (dest)[0] = (a)[0] + scale * (b)[0]; \
  85.     (dest)[1] = (a)[1] + scale * (b)[1]; \
  86.     (dest)[2] = (a)[2] + scale * (b)[2]; \
  87. }
  88. #define VectorLength(a)  sqrt((double) ((double)((a)[0] * (a)[0]) + (double)( (a)[1] * (a)[1]) + (double)( (a)[2] * (a)[2])) )
  89. #define VectorCompareMinimum(a,b,c) { (c)[0] = min((a)[0], (b)[0]); (c)[1] = min((a)[1], (b)[1]); (c)[2] = min((a)[2], (b)[2]); }
  90. #define VectorCompareMaximum(a,b,c) { (c)[0] = max((a)[0], (b)[0]); (c)[1] = max((a)[1], (b)[1]); (c)[2] = max((a)[2], (b)[2]); }
  91.  
  92. inline vec_t   VectorNormalize(vec3_t v)
  93. {
  94.     double          length;
  95.  
  96.     length = DotProduct(v, v);
  97.     length = sqrt(length);
  98.     if (length < NORMAL_EPSILON)
  99.     {
  100.         VectorClear(v);
  101.         return 0.0;
  102.     }
  103.  
  104.     v[0] /= length;
  105.     v[1] /= length;
  106.     v[2] /= length;
  107.  
  108.     return length;
  109. }
  110.  
  111. inline bool     VectorCompare(const vec3_t v1, const vec3_t v2)
  112. {
  113.     int             i;
  114.  
  115.     for (i = 0; i < 3; i++)
  116.     {
  117.         if (fabs(v1[i] - v2[i]) > EQUAL_EPSILON)
  118.         {
  119.             return false;
  120.         }
  121.     }
  122.     return true;
  123. }
  124.  
  125.  
  126. //
  127. // Portable bit rotation
  128. //
  129.  
  130.  
  131. #ifdef SYSTEM_POSIX
  132. #undef rotl
  133. #undef rotr
  134.  
  135. inline unsigned int rotl(unsigned value, unsigned int amt)
  136. {
  137.     unsigned        t1, t2;
  138.  
  139.     t1 = value >> ((sizeof(unsigned) * CHAR_BIT) - amt);
  140.  
  141.     t2 = value << amt;
  142.     return (t1 | t2);
  143. }
  144.  
  145. inline unsigned int rotr(unsigned value, unsigned int amt)
  146. {
  147.     unsigned        t1, t2;
  148.  
  149.     t1 = value << ((sizeof(unsigned) * CHAR_BIT) - amt);
  150.  
  151.     t2 = value >> amt;
  152.     return (t1 | t2);
  153. }
  154. #endif
  155.  
  156.  
  157. //
  158. // Misc
  159. //
  160.  
  161.  
  162. inline bool    isPointFinite(const vec_t* p)
  163. {
  164.     if (finite(p[0]) && finite(p[1]) && finite(p[2]))
  165.     {
  166.         return true;
  167.     }
  168.     return false;
  169. }
  170.  
  171.  
  172. //
  173. // Planetype Math
  174. //
  175.  
  176.  
  177. typedef enum
  178. {
  179.     plane_x = 0,
  180.     plane_y,
  181.     plane_z,
  182.     plane_anyx,
  183.     plane_anyy,
  184.     plane_anyz
  185. }
  186. planetypes;
  187.  
  188. #define last_axial plane_z
  189.  
  190. inline planetypes PlaneTypeForNormal(vec3_t normal)
  191. {
  192.     vec_t           ax, ay, az;
  193.  
  194.     ax = fabs(normal[0]);
  195.     if (ax == 1.0)
  196.     {
  197.         return plane_x;
  198.     }
  199.  
  200.     ay = fabs(normal[1]);
  201.     if (ay == 1.0)
  202.     {
  203.         return plane_y;
  204.     }
  205.  
  206.     az = fabs(normal[2]);
  207.     if (az == 1.0)
  208.     {
  209.         return plane_z;
  210.     }
  211.  
  212.     if ((ax > ay) && (ax > az))
  213.     {
  214.         return plane_anyx;
  215.     }
  216.     if ((ay > ax) && (ay > az))
  217.     {
  218.         return plane_anyy;
  219.     }
  220.     return plane_anyz;
  221. }
  222.  
  223. #endif //MATHLIB_H__
  224.